
var project_name = ''
var json = JSON.parse(document.cookie);
var user = json.user;
var pwd = json.pwd;
var url_now = '/'
var project_config;

function simplifyPath(path) {
    const parts = path.split('/');
    const stack = [];
  
    for (let part of parts) {
      if (part === '' || part === '.') {
        continue;
      } else if (part === '..') {
        stack.pop();
      } else {
        stack.push(part);
      }
    }
  
    return '/' + stack.join('/');
  }

function review_preview() {
    var page = document.getElementById('preview');
    page.src =  debug_server + '/' + user + '/' + project_name +'/html' + url_now
}

function load_html_dir(url) {
    try {
        var xhr = new XMLHttpRequest();
        xhr.open("POST",remote+"/list_html_project/"+user+"/"+pwd+"/"+project_name, true);
        xhr.send(url);
        url_now = url;
        document.getElementById('title_file').innerText = '操作文件: '+url_now
        xhr.onload=function() {
            console.log(xhr.responseText)
            var get_json = JSON.parse(xhr.responseText);
            console.log(get_json)
            document.getElementById('file_m').innerHTML = ''
            for (var key in get_json) {
                (function(url , key) {
                    var dom = document.createElement('button');
                    document.getElementById('file_m').appendChild(dom);
                    dom.className='b_3'
                    dom.innerText = get_json[key] + ": "+key;
                    dom.id = String(url+ "/" + key).replaceAll('//','/')
                    if (key == 'index.html') {
                        dom.innerText = "(首页文件) "+get_json[key] + ": "+key;
                    }
                    dom.onclick=function() {
                        url_now = dom.id;
                        review_preview();
                        document.getElementById('title_file').innerText = '操作文件: '+url_now
                        if (get_json[key] == '目录') {
                            load_html_dir(url_now)
                        }
                    }
                }) (url_now , key)
            }
        }
    }
    catch(e) {
        showAlert("网络或者程序错误" , null)
    }
}

window.onload = function() {
    var url = window.location.href;
    var split_url = url.split("?");
    var map = new Map();
    for (var i = 0 ; i < split_url.length ;i++) {
        try{
            var split_value = split_url[i].split("=");
            map[split_value[0]] = split_value[1];
        }catch(e) {
            continue;
        }
    }
    project_name = map['name'];
    load_html_dir(url_now);

    // 获取项目配置
    var xhr_project_config = new XMLHttpRequest();
    xhr_project_config.open('GET' , remote+'/get_project_config/'+user+'/'+pwd+'/'+project_name);
    xhr_project_config.send();
    xhr_project_config.onload = function() {
        project_config = JSON.parse(xhr_project_config.responseText);
        if (project_config['Type'] == 'html_website') {
            document.getElementById('app_server_control').style.display='none'
        }
        document.getElementById('title_file').innerText = '操作文件: '+url_now
        document.getElementById('title_name').innerText = '管理项目: '+project_config['Name']
        var page = document.getElementById('preview');
        page.src = debug_server + '/' + user + '/' + project_name +'/html/'
    }
}